//
//  GYQuestionCell.m
//  research
//
//  Created by 章鱼 on 17/3/27.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYQuestionCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface GYQuestionCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *infoLable;
@property (nonatomic, strong) UILabel *timeLable;
@property (nonatomic, strong) UILabel *statusLable;
@property (nonatomic, strong) UILabel *status;
@property (nonatomic, strong) UIImageView *likeView;
@property (nonatomic, strong) UILabel *likeNumber;
@property (nonatomic, strong) UIImageView *readView;
@property (nonatomic, strong) UILabel * readNumber;
@property (nonatomic, strong) UIView *emptyView;


@end


@implementation GYQuestionCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style
              reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initSubUI];
    }
    return self;
}

- (void)initSubUI {
    _emptyView = [[UIView alloc] init];
    [self.contentView addSubview:_emptyView];
    _emptyView.backgroundColor = [UIColor orz_globalBackground];
    
    _bgView = [[UIView alloc] init];
    [self.contentView addSubview:_bgView];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.masksToBounds = YES;
    _bgView.layer.cornerRadius = 4;
   

    _pictureView = [[UIImageView alloc] init];
    _pictureView.backgroundColor = [UIColor orz_assistGreen];
    [_bgView addSubview:_pictureView];
    _pictureView.layer.masksToBounds = YES;
    _pictureView.layer.cornerRadius = 4;
    _pictureView.layer.borderColor = [UIColor whiteColor].CGColor;
    _pictureView.layer.borderWidth = 2.f;
    
    _titleLable = [[UILabel alloc] init];
    _titleLable.font = [UIFont systemFontOfSize:16];
    _titleLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    [_bgView addSubview:_titleLable];
    
    _infoLable = [[UILabel alloc] init];
    _infoLable.font = [UIFont systemFontOfSize:12];
    _infoLable.textColor = [UIColor colorWithHex:@"646464" alpha:1];
    [_bgView addSubview:_infoLable];

    
    _timeLable = [[UILabel alloc] init];
    _timeLable.font = [UIFont systemFontOfSize:11];
    _timeLable.textColor = [UIColor colorWithHex:@"909090" alpha:1];
    [_bgView addSubview:_timeLable];

    _statusLable = [[UILabel alloc] init];
    _statusLable.font = [UIFont systemFontOfSize:12];
    _statusLable.textColor = [UIColor orz_textAssist];
    [_bgView addSubview:_statusLable];
    _statusLable.text = @"状态:";

    _status = [[UILabel alloc] init];
    _status.font = [UIFont systemFontOfSize:12];
    _status.textColor = [UIColor colorWithHex:@"ffcfc5" alpha:1];
    [_bgView addSubview:_status];
    _status.text =@"开放";
    
    _likeView = [[UIImageView alloc] init];
    [_bgView addSubview:_likeView];
    [_likeView setImage:[UIImage imageNamed:@"home_icon_Collection"]];
    
    _likeNumber = [[UILabel alloc] init];
    _likeNumber.font = [UIFont systemFontOfSize:11];
    _likeNumber.textColor = [UIColor orz_textAssist];
    _likeNumber.text = @"100";
    [_bgView addSubview:_likeNumber];
    
    _readView = [[UIImageView alloc] init];
    [_bgView addSubview:_readView];
    [_readView setImage:[UIImage imageNamed:@"home_icon_browse"]];
    
    _readNumber = [[UILabel alloc] init];
    _readNumber.font = [UIFont systemFontOfSize:11];
    _readNumber.text = @"200";
    _readNumber.textColor = [UIColor orz_textAssist];
    [_bgView addSubview:_readNumber];
    
    
    [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(0.f);
        make.height.mas_equalTo(7.f);
    }];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(11.f);
        make.right.mas_equalTo(-11.f);
        make.top.mas_equalTo(_emptyView.mas_bottom);
        make.bottom.mas_equalTo(0.f);
    }];
    
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_bgView).offset(10.f);
        make.left.mas_equalTo(_bgView).offset(10.f);
        make.right.mas_equalTo(_bgView).offset(-7.f);
        make.height.mas_equalTo(140 *ADJUST_SCREEN_HEIGHT_SCALE);
    }];
    
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.f);
        make.top.mas_equalTo(_pictureView.mas_bottom).offset(5.f);
        make.right.mas_equalTo(-70.f);
        

    }];
    
    [_infoLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.f);
        make.top.mas_equalTo(_titleLable.mas_bottom).offset(5.f);
        make.right.mas_equalTo(-10.f);
    }];
    
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10.f);
        make.top.mas_equalTo(_infoLable.mas_bottom).offset(5.f);
        make.bottom.mas_equalTo(_bgView).offset(-10.f);
    }];
    
    [_status mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-11.f);
        make.centerY.mas_equalTo(_titleLable.mas_centerY);
    }];
    
    [_statusLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_status.mas_left);
        make.centerY.mas_equalTo(_titleLable.mas_centerY);
    }];
    
    [_likeNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-10.f);
        make.right.mas_equalTo(-11.f);
        
    }];
    
    [_likeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_likeNumber.mas_centerY);
        make.right.mas_equalTo(_likeNumber.mas_left).offset(-3.f);
    }];
    
    [_readNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_likeNumber.mas_centerY);
        make.right.mas_equalTo(_likeView.mas_left).offset(-20.f);
        
    }];
    [_readView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_likeNumber.mas_centerY);
        make.right.mas_equalTo(_readNumber.mas_left).offset(-3.f);
        
    }];
    
}
- (void)updateCellWithData:(GYQuestInfoModel *)data{
    _titleLable.text = data.title;
    _infoLable.text = data.summary;
    [_pictureView sd_setImageWithURL:[NSURL URLWithString:data.picture] placeholderImage:nil];
    

    NSString * timeStampString = data.createTime;
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyy-MM-dd"];
    _timeLable.text = [NSString stringWithFormat:@"发表于：%@",[objDateformat stringFromDate: date]];
    
    
    if (data.status == 0 ) {
        _status.text = @"未开始";
        _status.textColor = [UIColor grayColor];
        NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",_status.text]];
        [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
        _status.attributedText = newPrice;

    }else if (data.status == 1 ){
        _status.text = @"运行中";
        _status.textColor = [UIColor colorWithHex:@"ffcf5c" alpha:1];

    }else if (data.status == 2 ){
        _status.text = @"已结束";
        _status.textColor = [UIColor grayColor];
        
    }
    _readNumber.text = [NSString stringWithFormat:@"%ld",(long)data.readCount];
    _likeNumber.text = [NSString stringWithFormat:@"%ld",(long)data.sort];

}

@end
